package com.boc.address.presenter;

import com.boc.address.bean.AddressListBean;
import com.boc.address.contract.AddressListContract;
import com.boc.address.model.AddressListModel;

import java.util.List;

import demo.bocweb.com.sdk.base.bean.BaseBean;
import demo.bocweb.com.sdk.baseApi.AppUrl;
import demo.bocweb.com.sdk.baseApi.BaseErrorObserver;
import demo.bocweb.com.sdk.baseApi.BaseObserver;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToastyUtil;
import okhttp3.RequestBody;

/**
 * Created by zhelun on 2018/2/2.
 */

public class AddressListPresenter extends AddressListContract.IAddressListPresenter {
    @Override
    public void getAddressList() {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.getAddressList()
                .subscribe(new BaseObserver<BaseBean<List<AddressListBean>>>() {
                    @Override
                    public void setData(BaseBean<List<AddressListBean>> beanBaseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (beanBaseBean.getReturnCode() == AppUrl.SuccessCode) {
                            mIView.updateAddressList(beanBaseBean.getData());
                        } else {
                            ToastyUtil.showInfo(beanBaseBean.getMsg());
                        }

                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public void setDefaultAddress(RequestBody request) {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.setDefaultAddress(request)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean beanBaseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (beanBaseBean.getReturnCode() == AppUrl.SuccessCode) {
                            ToastyUtil.showSuccess(beanBaseBean.getMsg());
                            mIView.setDefaultSuccess();
                        } else {
                            ToastyUtil.showInfo(beanBaseBean.getMsg());
                        }

                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public void deleteAddress(RequestBody request) {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.deleteAddress(request)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean beanBaseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (beanBaseBean.getReturnCode() == AppUrl.SuccessCode) {
                            ToastyUtil.showSuccess(beanBaseBean.getMsg());
                            mIView.deleteSuccess();
                        } else {
                            ToastyUtil.showInfo(beanBaseBean.getMsg());
                        }

                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public AddressListContract.IAddressListModel getModel() {
        return AddressListModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
